package com.henushang.ranking.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.ui.Model;

import com.henushang.ranking.domain.CategoryInfo;
import com.henushang.ranking.domain.Constants;
import com.henushang.ranking.domain.ResponseCode;
import com.henushang.ranking.domain.ResponseVO;
import com.henushang.ranking.domain.User;
import com.henushang.ranking.service.CategoryService;

public abstract class BaseController {

    protected final String PAGE_404 = "404";
    protected final String PREFIX_JOB_INFO = "job_info/";

    protected void addNavData(Model model, CategoryService categoryService) {
        List<CategoryInfo> navCates = categoryService.getNavCategory();
        model.addAttribute("nav_cate", navCates);
    }

    protected User getCurUser(HttpServletRequest request) {
        Object object = request.getSession().getAttribute(Constants.CUR_USER);
        return object == null ? null : (User) object;
    }

    protected String getCurUserId(HttpServletRequest request) {
        User user = getCurUser(request);
        if (user == null) {
            return null;
        }
        return user.get_id();
    }

    protected boolean addUser2Session(HttpServletRequest request, User user) {
        HttpSession session = request.getSession();
        session.setAttribute(Constants.CUR_USER, user);
        return true;
    }

    protected boolean removeUserFromSession(HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.removeAttribute(Constants.CUR_USER);
        return true;
    }

    protected ResponseVO errorVO(String msg) {
        ResponseVO vo = new ResponseVO();
        vo.setStatus(ResponseCode.ERROR);
        vo.setMsg(msg);
        return vo;
    }

    protected ResponseVO successVO() {
        ResponseVO vo = new ResponseVO();
        vo.setStatus(ResponseCode.SUCCESS);
        return vo;
    }
}
